package com.epam.web_project.bank.command.customer_commands;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.epam.web_project.bank.command.Command;
import com.epam.web_project.bank.dao.account_dao.AccountDAO;
import com.epam.web_project.bank.dao.factory.MySQLDAOFactory;
import com.epam.web_project.bank.dao.transaction_dao.TransactionDAO;
import com.epam.web_project.bank.db.ConnectionPool;
import com.epam.web_project.bank.entity.Account;
import com.epam.web_project.bank.entity.Currency;
import com.epam.web_project.bank.entity.Transaction;
import com.epam.web_project.bank.util.AppPropertiesLoader;

public class TransferCommand implements Command {
	private static AppPropertiesLoader propLoader = AppPropertiesLoader
			.getInstance();

	//transfer money
	@Override
	public String execute(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String cardNumber = request.getParameter("card");
		String firstName = request.getParameter("f_name");
		String lastName = request.getParameter("l_name");
		MySQLDAOFactory daoFactory = new MySQLDAOFactory();
		daoFactory.setConnectionPool(ConnectionPool.getInstance());
		AccountDAO accountDAO = daoFactory.getAccountDAO();
		Account accountReceiver = accountDAO.findByCustomerNameAndCard(
				firstName, lastName, cardNumber);
		HttpSession session=request.getSession();
		if (accountReceiver != null) { // the name and iban are correct
			Transaction transaction = new Transaction();
			Account accountSender = accountDAO.findById(Integer.valueOf(request
					.getParameter("senderAccId")));
			BigDecimal amountSent = new BigDecimal(
					request.getParameter("amount"));
			transaction.setAmountSent(amountSent);
			transaction.setNote(request.getParameter("note"));
			BigDecimal amountReceived = amountReceived(accountSender,
					accountReceiver, amountSent);
			transaction.setAmountReceived(amountReceived);
			transaction.setAccountFromId(accountSender.getId());
			transaction.setAccountToId(accountReceiver.getId());
			TransactionDAO transactionDAO = daoFactory.getTransactionDAO();
			String message = transactionDAO.save(transaction);
			session.setAttribute("message", message);
		} else {
			Locale locale=request.getLocale();
			ResourceBundle bundle=ResourceBundle.getBundle("i18n", locale);
			session.setAttribute("message",
					bundle.getString("java.transf.wrong_data"));
		}
		return propLoader.getProperty("show_main_command");

	}

	private BigDecimal amountReceived(Account sender, Account receiver,
			BigDecimal amountSent) {
		BigDecimal res = null;
		BigDecimal eurToUsd = new BigDecimal(propLoader.getProperty("EURToUSD"));
		BigDecimal eurToKzt = new BigDecimal(propLoader.getProperty("EURToKZT"));
		BigDecimal usdToKzt = new BigDecimal(propLoader.getProperty("USDToKZT"));
		switch (sender.getCurrency()) {
		case EUR:
			if (receiver.getCurrency() == Currency.EUR) {
				res = amountSent;
			} else if (receiver.getCurrency() == Currency.USD) {
				res = amountSent.multiply(eurToUsd);
			} else if (receiver.getCurrency() == Currency.KZT) {
				res = amountSent.multiply(eurToKzt);
			}
			break;
		case USD:
			if (receiver.getCurrency() == Currency.EUR) {
				res = amountSent.divide(eurToUsd, 4, RoundingMode.HALF_UP);
			} else if (receiver.getCurrency() == Currency.USD) {
				res = amountSent;
			} else if (receiver.getCurrency() == Currency.KZT) {
				res = amountSent.multiply(usdToKzt);
			}
			break;
		case KZT:
			if (receiver.getCurrency() == Currency.EUR) {
				res = amountSent.divide(eurToKzt, 4, RoundingMode.HALF_UP);
			} else if (receiver.getCurrency() == Currency.USD) {
				res = amountSent.divide(usdToKzt, 4, RoundingMode.HALF_UP);
			} else if (receiver.getCurrency() == Currency.KZT) {
				res = amountSent;
			}
			break;
		}
		return res;
	}

}
